#include <stdio.h>
#include <gtk/gtk.h>
#include "main.h"
#include "widgets.h"
#include "get_crypto_price.h"

gboolean has_initialized = FALSE;


gboolean update(gpointer data)
{
	FILE * tickers_csv = fopen(file_address, "r");
	if ( tickers_csv == NULL )
	{
		printf("Failed to open: %s\n", file_address);
		gtk_main_quit();
		return FALSE;
	}
	char line_buffer[1024];
	char ticker_buffer[8];
	double price;
	double holding;
	double total_equity = 0;

	GDateTime * now = g_date_time_new_now_local();
	gchar * time_string = g_date_time_format(now, "%H:%M:%S");

	GString * market_output = g_string_new(NULL);
	g_string_printf(market_output, "                 Last update: %s \nMarket\n", time_string);
	GString * portfolio_output = g_string_new("\nPortfolio\n");
	GString * total_output = g_string_new("");

	while ( fgets(line_buffer, 1024, tickers_csv) != NULL)
	{
		if ( sscanf(line_buffer, "%[^,],%lf", ticker_buffer, &holding) != 2 )
		{
			continue;
		}
		// Get crypto price
		if ( strcmp(ticker_buffer, "AUD-AUD") == 0 )
		{
			price = 1;
		}
		else
		{
			price = get_crypto_price(ticker_buffer);
		}
		if ( price == -1 )
		{
			g_date_time_unref(now);
			g_free(time_string);
			g_string_free(market_output, TRUE);
			g_string_free(portfolio_output, TRUE);
			g_string_free(total_output, TRUE);
			fclose(tickers_csv);
			if ( has_initialized )
			{
				return G_SOURCE_CONTINUE;
			}
			else
			{
				has_initialized = TRUE;
				return G_SOURCE_REMOVE;
			}
		}
		// Update outputs
		if ( strcmp(ticker_buffer, "AUD-AUD") != 0 )
		{
			g_string_append_printf(market_output, " %s : $ %11.4lf\n", ticker_buffer, price );
		}
		g_string_append_printf(portfolio_output, " %s | %11.2lf | $ %11.2lf\n", ticker_buffer, holding, holding * price);
		total_equity += holding * price;
	}

	g_string_printf(total_output, "%s %s \nTotal:                   $ %11.2lf", market_output->str, portfolio_output->str, total_equity);

	gtk_label_set_text(GTK_LABEL(output), total_output->str);

	g_date_time_unref(now);
	g_free(time_string);
	g_string_free(market_output, TRUE);
	g_string_free(portfolio_output, TRUE);
	g_string_free(total_output, TRUE);
	
	fclose(tickers_csv);
	if ( has_initialized )
	{
		return G_SOURCE_CONTINUE;
	}
	else
	{
		has_initialized = TRUE;
		return G_SOURCE_REMOVE;
	}
}

int initial_update()
{
	FILE * tickers_csv = fopen(file_address, "r");
	if ( tickers_csv == NULL )
	{
		printf("Failed to open: %s\n", file_address);
		return -1;
	}
	char line_buffer[1024];
	char ticker_buffer[8];
	double price;

	GString * market_output = g_string_new("                 Last update: 00:00:00 \nMarket\n");
	GString * portfolio_output = g_string_new("\nPortfolio\n");
	GString * total_output = g_string_new("");

	while ( fgets(line_buffer, 1024, tickers_csv) != NULL )
	{
		if ( sscanf(line_buffer, "%[^,]", ticker_buffer) != 1 )
		{
			continue;
		}
		if ( strcmp(ticker_buffer, "AUD-AUD") != 0 )
			{
				g_string_append_printf(market_output, " %s : $ %11.4lf\n", ticker_buffer, 0 );
			}
		g_string_append_printf(portfolio_output, " %s | %11.2lf | $ %11.2lf\n", ticker_buffer, 0, 0);
	}
	g_string_printf(total_output, "%s %s \nTotal:                   $ %11.2lf", market_output->str, portfolio_output->str, 0);

	gtk_label_set_text(GTK_LABEL(output), total_output->str);

	g_string_free(market_output, TRUE);
	g_string_free(portfolio_output, TRUE);
	g_string_free(total_output, TRUE);
	
	fclose(tickers_csv);

    g_timeout_add(10000, update, NULL);
}


